<template>
    <div class="header">
        <div class="title">
            <img src="../assets/logo2.png" alt="照片">
        </div>
        <el-dropdown trigger="click">
            <i class="el-icon-setting" style="margin-right: 5px; line-height:80px"></i>
            <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="changePass">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>

        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                     class="demo-ruleForm">

                <el-form-item label="原密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="newPassword">
                    <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm">提交</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>

</template>

<script>
    import {serverIp} from "../../public/config";

    export default {
        name: "Header",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                serverIp:serverIp,
                users: JSON.parse(sessionStorage.getItem('CurUser')),
                dialogVisible: false,
                ruleForm: {
                    studentId: '',
                    password: '',
                    newPassword: '',
                    checkPass: '',
                },
                rules: {
                    password: [
                        {required: true, message: '请输入原密码', trigger: 'submit'},
                        {min: 4, max: 16, message: '长度为4~16个字符', trigger: 'blur'},
                    ],
                    newPassword: [
                        {required: true, message: '请输入新密码', trigger: 'submit'},
                        {min: 4, max: 16, message: '长度为4~16个字符', trigger: 'blur'},
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {required: true, message: '请再次输入新密码', trigger: 'submit'},
                        {min: 4, max: 16, message: '长度为4~16个字符', trigger: 'blur'},
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            submitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        this.$axios.post('http://'+ serverIp +':8090/users/updatePassword', this.ruleForm)
                            .then(response => {
                                // 在此处处理响应结果
                                if (response.data.code === 200) {
                                    this.$message.success('密码修改成功，请重新登陆！！');
                                    this.centerDialogVisible = false;
                                    this.$router.push("/");
                                    sessionStorage.clear();
                                } else {
                                    this.$message.error('原密码错误，请重新输入密码！');
                                }
                            })
                            .catch(error => {
                                // 在此处处理异常情况
                                console.log(error);
                                this.$message.error('出现错误，请稍后重试');
                            });
                    } else {
                        console.log('error!!!');
                        return false;
                    }
                })
            },

            resetForm() {
                this.ruleForm.password = '';
                this.ruleForm.newPassword = '';
                this.ruleForm.checkPass = '';
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },

            changePass() {
                this.dialogVisible = true;
                this.ruleForm.studentId = this.users.studentId;
            },
            logout() {
                console.log("logout");
                this.$confirm('您确定要退出登录吗?', '提示', {
                    confirmButtonText: '确定',  //确认按钮的文字显示
                    type: 'warning',
                    center: true, //文字居中显示

                })
                    .then(() => {
                        this.$message({
                            type: 'success',
                            message: '退出登录成功'
                        });

                        this.$router.push("/");
                        sessionStorage.clear()
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消退出登录'
                        })
                    })
            }
        },

        created() {
            this.$router.push("/Home")
        }
    }
</script>

<style scoped>
    .el-dropdown {
        height: 80px;
        color: #fff
    }

    .el-dropdown div {
        display: flex;
        align-items: center;
        height: 80px;
    }

    .el-dropdown .el-dropdown-item {
        color: white;
    }

    .title img {
        height: 80px;
    }

    .title {
        flex: 1
    }

    .header {
        display: flex;
        text-align: center
    }

    .el-dropdown-item i {
        margin-right: 5px
    }
</style>